import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.event.*;
import javax.swing.ListSelectionModel;

public class StudentGUI extends JFrame implements ActionListener{


        private JButton addButton, editButton, deleteButton, uploadButton, downloadButton, saveButton, exitButton, logoutButton;
        private JScrollPane scroll;

        protected static JTable tableStudList;
        protected static StudentTableModel model;
        private ListSelectionModel listSelectionModel;
        private JPanel tablePanel = new JPanel();



        public StudentGUI(){


                addButton = new JButton("Add");
                editButton = new JButton("Edit");
                editButton.setEnabled(false);
                deleteButton = new JButton("Delete");
                deleteButton.setEnabled(false);
                uploadButton = new JButton("Upload a File");
                downloadButton = new JButton("Download a File");
                saveButton = new JButton("Save");
                exitButton = new JButton("Close");
                logoutButton = new JButton("Log Out");


                String[] columnNames = {"ID", "NAME", "COURSE", "YEAR", "CODE", "SCHEDULE"};
                Object[][] data = StudentMainClass.data;

                //Initialize String columnNames and Object data[][] for JTable
                model = new StudentTableModel(data, columnNames);

                tableStudList = new JTable(model);

                //Adds JTable to JScrollPane
                scroll = new JScrollPane(tableStudList);
                tablePanel.add(scroll);


                JPanel buttonPanel = new JPanel(new GridLayout(0,5,10,10));
                buttonPanel.add(addButton);
                buttonPanel.add(editButton);
                buttonPanel.add(deleteButton);
                buttonPanel.add(uploadButton);
                buttonPanel.add(downloadButton);
                buttonPanel.add(saveButton);
                buttonPanel.add(exitButton);
                buttonPanel.add(logoutButton);

                listSelectionModel = tableStudList.getSelectionModel();
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());

                //addActionListeners
                addButton.addActionListener(this);
                editButton.addActionListener(this);
                deleteButton.addActionListener(this);
                uploadButton.addActionListener(this);
                downloadButton.addActionListener(this);
                saveButton.addActionListener(this);
                exitButton.addActionListener(this);
                logoutButton.addActionListener(this);


                //Set the layout of the JFrame with GridBagLayout and GridBagConstraints
                setLayout(new GridBagLayout());
                GridBagConstraints constraint = new GridBagConstraints();

                constraint.fill = GridBagConstraints.HORIZONTAL;
                constraint.weightx = 1;
                constraint.weighty = 1;
                constraint.insets = new Insets(2,2,2,2);
                add(tablePanel, constraint);

                constraint.weightx = 0;
                constraint.weighty = 0;
                constraint.gridy = 2;
                constraint.insets = new Insets(2,5,5,5);
                add(buttonPanel, constraint);

                //Sets the variables of the JFrame
                pack();
                setTitle("Student Record Database");
                setVisible(true);
                setResizable(true);
                setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        //ActionEvent from the JButtons
        public void actionPerformed(ActionEvent e){
                if(e.getSource() == addButton){
                        JDialog jdialogAdd = new ButtonDialog(this,"ADD");


                }

                else if(e.getSource() == editButton){
                        JDialog jdialogEdit = new ButtonDialog(this,"EDIT");

                }

                else if(e.getSource() == deleteButton){
                        int confirm = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
                        JOptionPane.YES_NO_OPTION);

            if (confirm == 0) {
                 model.removeRow(tableStudList.getSelectedRow());
            }

                }
                else if(e.getSource() == uploadButton){
                                Uploader up = new Uploader();
                        up.setVisible(true);
                        dispose();
                up.setSize(270,180);
                up.setLocation(500,250);
                }

                else if(e.getSource() == saveButton){
                        StudentMainClass.saveData();

                }

                else if(e.getSource() == logoutButton){
                        Login login = new Login();
                        login.setVisible(true);
                dispose();
                        login.setSize(270,180);
                        login.setLocation(500,250);

                }

                 else if(e.getSource() == downloadButton){
					Downloader download = new Downloader();
           			 download .setVisible(true);
           			 dispose();
           			 download.setSize(800,600);

            }




                else if(e.getSource() == exitButton){
                        int confirm = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
                        JOptionPane.YES_NO_OPTION);

            if (confirm == 0) {
                 System.exit(0);
            }
                }
        }

        //Handles the functionalities of the edit and delete button when a part of the table is selected

        class SharedListSelectionHandler implements ListSelectionListener {
                 public void valueChanged(ListSelectionEvent e) {
                        editButton.setEnabled(true);
                        deleteButton.setEnabled(true);
                 }
        }

        class StudentTableModel extends DefaultTableModel{
                public StudentTableModel(Object rowData[][], Object columnNames[]){
                super(rowData, columnNames);
        }

                public boolean isCellEditable(int row, int column) {
                return false;
                }
        }
}